﻿using UnityEditor;
using UnityEngine;

namespace HKTools
{
    [CustomEditor(typeof(HK_SimpleFrameSequence))]
    public class HK_SimpleFrameSequenceEditor : Editor
    {
        public override void OnInspectorGUI()
        {
            // 绘制原组件的默认Inspector界面
            base.OnInspectorGUI();

            // 获取目标组件实例
            HK_SimpleFrameSequence targetSeq = (HK_SimpleFrameSequence)target;

            EditorGUILayout.Space(6);

            // "关闭Mipmap"按钮
            if (GUILayout.Button("关闭所有序列帧的 Mipmap", GUILayout.Height(25)))
            {
                if (targetSeq.frames == null || targetSeq.frames.Length == 0)
                {
                    EditorUtility.DisplayDialog("提示", "序列帧数组未赋值或为空！", "确定");
                    return;
                }

                // 遍历所有Sprite并修改关联纹理的Mipmap设置
                foreach (Sprite sprite in targetSeq.frames)
                {
                    if (sprite == null) continue;
                    ModifyTextureMipmap(sprite.texture);
                }

                AssetDatabase.Refresh(); // 刷新资源数据库
                EditorUtility.DisplayDialog("完成", "已关闭所有关联纹理的Mipmap！", "确定");
            }

            // "禁用Read/Write"按钮
            if (GUILayout.Button("禁用所有序列帧的 Read/Write Enabled", GUILayout.Height(25)))
            {
                if (targetSeq.frames == null || targetSeq.frames.Length == 0)
                {
                    EditorUtility.DisplayDialog("提示", "序列帧数组未赋值或为空！", "确定");
                    return;
                }

                // 遍历所有Sprite并修改关联纹理的Read/Write设置
                foreach (Sprite sprite in targetSeq.frames)
                {
                    if (sprite == null) continue;
                    ModifyTextureReadWrite(sprite.texture);
                }

                AssetDatabase.Refresh(); // 刷新资源数据库
                EditorUtility.DisplayDialog("完成", "已禁用所有关联纹理的Read/Write Enabled！", "确定");
            }
        }

        /// <summary>
        /// 修改纹理的Mipmap设置（关闭）
        /// </summary>
        void ModifyTextureMipmap(Texture2D texture)
        {
            if (texture == null) return;
            string texturePath = AssetDatabase.GetAssetPath(texture);
            if (string.IsNullOrEmpty(texturePath)) return;

            TextureImporter importer = TextureImporter.GetAtPath(texturePath) as TextureImporter;
            if (importer != null && importer.mipmapEnabled)
            {
                importer.mipmapEnabled = false;
                importer.SaveAndReimport(); // 保存并重新导入纹理
            }
        }

        /// <summary>
        /// 修改纹理的Read/Write设置（禁用）
        /// </summary>
        void ModifyTextureReadWrite(Texture2D texture)
        {
            if (texture == null) return;
            string texturePath = AssetDatabase.GetAssetPath(texture);
            if (string.IsNullOrEmpty(texturePath)) return;

            TextureImporter importer = TextureImporter.GetAtPath(texturePath) as TextureImporter;
            if (importer != null && importer.isReadable)
            {
                importer.isReadable = false;
                importer.SaveAndReimport(); // 保存并重新导入纹理
            }
        }
    }
}